<?php

namespace addons\zzyy\module;

use app\admin\model\zzyy\ZzyyFeature;

class Feature
{
    static function transTagStringsToIds($tagsString)
    {
        if (empty($tagsString)) {
            return [];
        }
        $tags = explode(',', $tagsString);
        $tagMap = [];

        foreach (ZzyyFeature::all() as $item) {
            $tagMap[$item->feature_name] = $item->id;
        }

        $ids = [];
        foreach ($tags as $v) {
            if (in_array($v, array_keys($tagMap))) {
                $ids[] = $tagMap[$v];
            } else {
                $modal = new ZzyyFeature();
                $ids[] = $modal->insertGetId([
                    'feature_name' => $v,
                    'createtime' => time(),
                    'updatetime' => time(),
                ]);
            }
        }

        return $ids;
    }

    static function transTagidsToTagStrings($tagids)
    {
        if (empty($tagids)) {
            return [];
        }
        $tagids = explode(',', $tagids);
        $tagMap = [];
        foreach (ZzyyFeature::all() as $item) {
            $tagMap[$item->id] = $item->feature_name;
        }
        return array_map(function ($e) use ($tagMap) {
            return $tagMap[$e];
        }, $tagids);
    }
}